define(function(require) {
	var $ = require('jquery');
	//var justep = require("$UI/system/lib/justep");
	var guid = require('../lib/guid'); 
	var comm = require('../lib/user/comm');
	require("../lib/mqttws31");
	
	
	var MqttBaitu = function() {
		
		//this.host = "bgw_0001.mqtt.iot.gz.baidubce.com";
		this.host = "homegateway.mqtt.iot.gz.baidubce.com";
		this.port = 8884;
		this.clientId = guid.newGUID().slice(0, 23); // 百度天工的 clientid 最大 23 位

		
		
		this.connected = justep.Bind.observable(false);
		
		
		this.mqttInfo = {
			//host: "bgw_0001.mqtt.iot.gz.baidubce.com",
			//clientId: "site-01-dev-02",
			//"port": 8884,
			//"username": "bgw_0001/t-01",
			//"password": "uje9hjxxopy8o69lcU+Skx14+1J16Rb8fGykY00S6TM=",
			"username": "homegateway/tlrm_serv",
			"password": "SPAE7T8GtYRMdT+0qzjnzBgvl/USuqSk9HrleUh5Zis=",
			"keepAlive": 60,
			"ssl": true,
			"cleanSession": true,
			"qos": 0,
			"retained": false,
		};

		this.mqclient = null;
		
		
		//---- event
		this.onConnect = null;
		this.onFail = null;
		this.onMsgArrived = null;
		this.onMsgArrivedStatus = null;
		this.onMsgArrivedGrating = null;
		this.onMsgArrivedAccelX = null;
		this.onMsgArrivedAccelSTX = null;

    	var self = this;
   		this.mqclient = new Messaging.Client(this.host, this.port, this.clientId);
        this.mqclient.onMessageArrived =  function(msg){ self.doMqttMessageArrived(msg); };
	
		//---- 1. connect
		this.Connect(false);
	};
	
	MqttBaitu.prototype.showErr = function(){
		
	};

	MqttBaitu.prototype.doMqttConnect = function () {
        this.connected.set(true);
    	
		//---- 2. Subscribe
    	this.Subscribe(comm.gParams.baiduTopics.accelX + '/#', 0);		// 最大数据量；5秒一个数据包
    	this.Subscribe(comm.gParams.baiduTopics.accelSTX + '/#', 0);
    	this.Subscribe(comm.gParams.baiduTopics.grating + '/#', 0);
    	this.Subscribe(comm.gParams.baiduTopics.status + '/#', 0);

    	if(typeof this.onConnect == 'function'){
    		this.onConnect();
    		//justep.Util.hint('connected');
    	}
    };

    MqttBaitu.prototype.doMqttFail = function (message) {
		justep.Util.hint('mqtt failed: ' + message.errorMessage);
        this.connected.set(false);
    	if(typeof this.onFail == 'function'){
    		this.onFail();
    	}
    };

    MqttBaitu.prototype.doMqttMessageArrived = function (message) {
    	if (message.destinationName.indexOf(comm.gParams.baiduTopics.accelX) >= 0){
        	if(typeof this.onMsgArrivedAccelX == 'function'){
            	//justep.Util.hint('mqtt message arrived: accelX');
        		this.onMsgArrivedAccelX(message);
        	}
    	}
    	else if (message.destinationName.indexOf(comm.gParams.baiduTopics.grating) >= 0){
        	if(typeof this.onMsgArrivedGrating == 'function'){
            	//justep.Util.hint('mqtt message arrived grating');
        		this.onMsgArrivedGrating(message);
        	}
    	}
    	else if (message.destinationName.indexOf(comm.gParams.baiduTopics.status) >= 0){
        	if(typeof this.onMsgArrivedStatus == 'function'){
            	//justep.Util.hint('mqtt message arrived status');
        		this.onMsgArrivedStatus(message);
        	}
    	}
    	else if (message.destinationName.indexOf(comm.gParams.baiduTopics.accelSTX) >= 0){
        	if(typeof this.onMsgArrivedAccelSTX == 'function'){
            	//justep.Util.hint('mqtt message arrived accelSTX');
        		this.onMsgArrivedAccelSTX(message);
        	}
    	}
    	
    	//----
    	if(typeof this.onMsgArrived == 'function'){
    		this.onMsgArrived(message);
    	}
    	//this.showMsg(message.destinationName + ": " + message.payloadString);
    };
    
    
    // reconnect = ture 表示当连接存在时依然重连
    MqttBaitu.prototype.Connect = function(reconnect){
    	var self = this;
    	
    	if (reconnect) {
    		this.Disconnect()
    	};
    	
    	if (this.connected.get()) {
    		return ;
    	};
    	
        var options = {
            timeout: 3,
            keepAliveInterval: this.mqttInfo.keepAlive,
            cleanSession: this.mqttInfo.cleanSession,
            useSSL: this.mqttInfo.ssl,
            onSuccess: function() {
            	self.doMqttConnect();
            },
            onFailure: function(message) {
            	self.doMqttFail(message)
            }
        };

        //----
        options.userName = this.mqttInfo.username
        options.password = this.mqttInfo.password;

        this.mqclient.connect(options);
    };

    MqttBaitu.prototype.Disconnect = function() {
        if(this.connected.get()){
            this.mqclient.disconnect();
            this.connected.set(false);
        }
    };
    
    MqttBaitu.prototype.Subscribe = function(topic, qos){
		if(this.connected.get()){
			//this.mclient.subscribe("topic-01", {qos: 0});
			this.mqclient.subscribe(topic, qos);
			//justep.Util.hint(topic + " qos =" + this.mqttInfo.qos);
		}
	};
	
    MqttBaitu.prototype.Unsubscribe = function(topic){
		if(this.connected.get()){
			this.mqclient.unsubscribe(topic);
		}
	};

	MqttBaitu.prototype.SendMsg = function(topic, qos, msg){
		var self = this;
		if(this.connected.get()) {
	        var message = new Messaging.Message(msg);
	        message.destinationName = topic;
	        message.qos = qos;
	        message.retained = false; //this.mqttInfo.retained;
	        this.mqclient.send(message);
		}
	};
	
	MqttBaitu.prototype.test = function(){
		 return "test string";
	};
	

	//return MqttBaitu;
	return new MqttBaitu();
});
